package com.timous.链表;

import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * https://leetcode-cn.com/problems/reverse-linked-list/
 * @author Timous
 * @create 2020-12-21 15:50
 **/
public class _206_反转链表 {
//递归的形式
//    public ListNode reverseList(ListNode head) {
//        //传过来的head是null  没必要反转
//        if (head == null){
//            return null;
//        }
//        if (head.next == null){
//            return head;
//        }
//        ListNode newHead = reverseList(head.next);
//        head.next.next = head;
//        head.next = null;
//
//        return newHead;
//    }
    //非递归的形式
    public ListNode reverseList(ListNode head) {
        ListNode tem = null;
        ListNode newHead = null;
        if (head == null){
            return null;
        }
        if (head.next == null){
            return head;
        }
        while (head != null){
            tem = head.next;
            head.next = newHead;
            newHead = head;
            head = tem;
        }
        return newHead;
    }

}
